from face import AipFace

class FaceApi(object):
    def __init__(self, appid, apikey, secretkey, configdict):
        self.client = AipFace(appid, apikey, secretkey)
        self.result = None
        self.config = configdict
        self.result_resi = None
        if self.config['therhold'] == None:
            self.therhold = 80
        else:
            self.therhold = self.config['therhold']
    
    def __call__(self, imgpath):
        with open(imgpath, 'rb') as f:
            scores = 0
            self.result = self.client.identifyUser(self.config['facegroup'], f.read())['result']
            for resi in self.result:
                try:
                    scores = resi['scores'][0]
                    if scores >= self.therhold:
                        self.result_resi = True
                    else:
                        self.result_resi = False
                except:
                    pass
        return self.result_resi, self.result, self.therhold, self.client, self.config